﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controllers;

namespace Web
{
    public partial class IMCoopschemeEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                var list = ctx.CMS_IMGrade.OrderBy(r => r.OrderNo);
                IMGradeId.DataSource = list;
                IMGradeId.DataTextField = "Grade";
                IMGradeId.DataValueField = "Id";
                IMGradeId.DataBind();
                IMGradeId.Items.Insert(0,new ListItem("",""));

                if (Request.Params["Id"] != null)
                {
                    int id = int.Parse(Request.Params["Id"]);

                    CMS_IMCoopscheme model = ctx.CMS_IMCoopscheme.Single(q => q.Id == id);
                    IMGradeId.Value = model.IMGradeId.ToString();
                    LowerQuantity.Value = model.LowerQuantity.ToString();
                    HigherQuantity.Value = model.HigherQuantity.ToString();
                    InvestorRate.Value = model.InvestorRate.ToString();
                    GuanXiangRate.Value = model.GuanXiangRate.ToString();
                    IMRate.Value = model.IMRate.ToString();
                    Remarks.Value = model.Remarks;
                    OrderNo.Value = model.OrderNo.ToString();
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            CMS_IMCoopscheme model;
            string id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                model = ctx.CMS_IMCoopscheme.Single(r => r.Id == int.Parse(id));
            }
            else
            {
                model = new CMS_IMCoopscheme();
                ctx.CMS_IMCoopscheme.InsertOnSubmit(model);
            }
            model.IMGradeId = int.Parse(IMGradeId.Value);
            model.LowerQuantity = int.Parse(LowerQuantity.Value);
            if (!string.IsNullOrWhiteSpace(HigherQuantity.Value))
            {
                model.HigherQuantity = int.Parse(HigherQuantity.Value);
            }
            model.InvestorRate = decimal.Parse(InvestorRate.Value);
            model.GuanXiangRate = decimal.Parse(GuanXiangRate.Value);
            model.IMRate = decimal.Parse(IMRate.Value);
            model.Remarks = Remarks.Value;
            model.OrderNo = int.Parse(OrderNo.Value);
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}